package com.allwees.support.fileservice.config;

import com.allwees.support.fileservice.FileException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@EqualsAndHashCode(callSuper = false)
@Configuration
@ConfigurationProperties(prefix = "file")
public class FileConfig {
    private String major;

    private String profile;

    private String sysBuckets;

    private String bsBuckets;

    private String userBucket;

    private String tempBucket;

    public void check4SysBucket(String bucket) {
        if (!isSysBucket(bucket)) {
            throw new FileException(bucket + "not a sys bucket!");
        }
    }

    public void check4BsBucket(String bucket) {
        if (!isBsBucket(bucket)) {
            throw new FileException(bucket + "not a bs bucket!");
        }
    }

    public boolean isSysBucket(String bucket) {
        if (StringUtils.isBlank(bucket) || StringUtils.isEmpty(sysBuckets)) {
            return false;
        }
        return ArrayUtils.contains(sysBuckets.split(","), bucket);
    }

    public boolean isBsBucket(String bucket) {
        if (StringUtils.isBlank(bucket) || StringUtils.isEmpty(bsBuckets)) {
            return false;
        }
        return ArrayUtils.contains(bsBuckets.split(","), bucket);
    }

    public boolean isTempBucket(String bucket) {
        return StringUtils.isNotBlank(tempBucket) && tempBucket.equals(bucket);
    }

    public String getRealBucket(String bucket) {
        if ("china".equalsIgnoreCase(profile)) {
            return bucket.endsWith("-o") ? bucket.substring(bucket.length(), bucket.length() - 1) : bucket;
        } else {
            return bucket.endsWith("-o") ? bucket : bucket + "-o";
        }
    }
}
